<?php
// +----------------------------------------------------------------------
// | Description: 商城接口
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\crm\model\Customer as CustomerModel;
use app\shop\model\Refund as RefundModel;
use app\shop\model\Order as OrderModel;
use app\mini\model\PersonCustomer;
use app\crm\model\TransferShop;
use think\facade\Db;
use app\Request;
use think\Env;

class ApiCrontab
{
    
    
	   
    // 创建收款单
    public function miniCreatOrder(Request $request)
    {
        $param = $request->param();
        if(empty($param['insert']))
        {
            return json(['msg'=>'缺少必传参数','code'=>0]);
        }else
        {
            $order = json_decode($param['insert'],1);
        }
        // 查询
        $info = Db::name('customer')
            ->alias('c') 
            ->where('c.id',$order['uid'])
            ->join('shop s', 'c.shop_id = s.shop_id') 
            ->where('c.id', $order['uid']) 
            ->field('c.id,c.shop_id,c.cj_money,s.shop_name, s.market_id, s.market_name, s.market_kingdee_code')
            ->find();
        $order['shop_id'] = $info['shop_id'];
        $order['shop_name'] = $info['shop_name'];
        $order['market_id'] = $info['market_id'];
        $order['market_name'] = $info['market_name'];
        $order['sales_organization'] = $info['market_kingdee_code'];
        $order['settlement_organization'] = $info['market_kingdee_code'];
        $order['sales_name'] = $info['market_name'];
        $order['settlement_name'] = $info['market_name'];
        // 你需要判定这个客户之前是不是新单 ；客户档案表中 cj_money 是否大于0如果大于0 显示补单，如果等于0 且本次订单收款金额大于等于500 就是新单 否则就是补单
        if($info['cj_money'] == 0 && $order['pay_amount'] >= 500)
        {
            $order['order_type'] = 1;
        }else{
            $order['order_type'] = 2;
        }
        // 查询销售员信息
        $sale = Db::name('shop_person')
            ->where('shop_id', $order['shop_id'])
            ->where('position_id', 'in', '10,9,8,7')
            ->where('status', 1)
            ->column('id,name', 'position_id');
        $selected_sale = null;
        // 优先选择店长
        if (isset($sale[7])) {
            $managers_sale = is_array(reset($sale[7])) ? $sale[7] : [ $sale[7] ];
            $randomIndex_sale = array_rand($managers_sale);
            $selected_sale = $managers_sale[$randomIndex_sale];
        } 
        // 选择店助
        elseif (isset($sale[10])) {
            $assistants_sale = is_array(reset($sale[10])) ? $sale[10] : [ $sale[10] ];
            $randomIndex_sale = array_rand($assistants_sale);
            $selected_sale = $assistants_sale[$randomIndex_sale];
        } 
        // 选择护理师
        elseif (isset($sale[9])) {
            $nurses_sale = is_array(reset($sale[9])) ? $sale[9] : [ $sale[9] ];
            $randomIndex_sale = array_rand($nurses_sale);
            $selected_sale = $nurses_sale[$randomIndex_sale];
        }
        // 选择主任
        elseif (isset($sale[8])) {
            $directors_sale = is_array(reset($sale[8])) ? $sale[8] : [ $sale[8] ];
            $randomIndex_sale = array_rand($directors_sale);
            $selected_sale = $directors_sale[$randomIndex_sale];
        } 
        $order['sale_id'] = $selected_sale['id'] ?? '';
        $order['sale_name'] = $selected_sale['name'] ?? '';
        
        $operator = Db::name('admin_user')
            ->alias('au')
            ->join('shop_person sp', 'au.shop_person_id = sp.id')
            ->where('au.shop_id', $order['shop_id'])
            ->where('au.status', 1)
            ->where('au.is_status', 0)
            ->where('sp.status', 1)
            ->where('sp.position_id', 'in', '10,9,8,7')
            ->column('au.id,au.realname','sp.position_id');
            
        $selected_operator = null;
        // 优先选择店助
        if (isset($operator[10])) {
            $assistants_operator = is_array(reset($operator[10])) ? $operator[10] : [ $operator[10] ];
            $randomIndex_operator = array_rand($assistants_operator);
            $selected_operator = $assistants_operator[$randomIndex_operator];
        } 
        // 选择店长
        elseif (isset($operator[7])) {
            $managers_operator = is_array(reset($operator[7])) ? $operator[7] : [ $operator[7] ];
            $randomIndex_operator = array_rand($managers_operator);
            $selected_operator = $managers_operator[$randomIndex_operator];
        } 
        // 选择护理师
        elseif (isset($operator[9])) {
            $nurses_operator = is_array(reset($operator[9])) ? $operator[9] : [ $operator[9] ];
            $randomIndex_operator = array_rand($nurses_operator);
            $selected_operator = $nurses_operator[$randomIndex_operator];
        }
        // 选择主任
        elseif (isset($operator[8])) {
            $directors_operator = is_array(reset($operator[8])) ? $operator[8] : [ $operator[8] ];
            $randomIndex_operator = array_rand($directors_operator);
            $selected_operator = $directors_operator[$randomIndex_operator];
        } 
        $order['operator_id'] = $selected_operator['id'] ?? '';
        $order['operator_name'] = $selected_operator['realname'] ?? '';
        //请求创建订单接口
        $refundModel = new RefundModel();
        $data  = $refundModel->addData($order);
        if(!$data)
        {
            return json(['msg'=> $refundModel->getError(),'code'=>0]);
        }else
        {
            // 查询订单ID
            $id = Db::name('shop_refund_order')->where('api_order_id',$order['api_order_id'])->value('id');
            return json(['data'=>$id, 'code'=>1]);
        }
    }
    
    
    
    // 创建出库单
    public function miniOrderGoods(Request $request)
    {
        $param = $request->param();
        if(empty($param['insert']))
        {
            return json(['msg'=>'缺少必传参数','code'=>0]);
        }else
        {
            $order = json_decode($param['insert'],1);
        }
        // 查询
        $info = Db::name('customer')
            ->alias('c') 
            ->where('c.id',$order['uid'])
            ->join('shop s', 'c.shop_id = s.shop_id') 
            ->join('shop_store e', 's.store_id = e.id') 
            ->field('c.id,c.shop_id,c.cj_money,s.shop_name, s.market_id, s.market_name, s.market_kingdee_code,s.store_name,e.kingdee_code')
            ->find();
        $order['shop_id'] = $info['shop_id'];
        $order['shop_name'] = $info['shop_name'];
        $order['market_id'] = $info['market_id'];
        $order['market_name'] = $info['market_name'];
        $order['sales_organization'] = $info['market_kingdee_code'];
        $order['settlement_organization'] = $info['market_kingdee_code'];
        $order['sales_name'] = $info['market_name'];
        $order['settlement_name'] = $info['market_name'];
        $order['delivery_king'] = $info['kingdee_code'];
        $order['house_code'] = $info['kingdee_code'];
        $order['house_code_name'] = $info['store_name'];
        $order['delivery_name'] = $info['shop_name'];
        // var_dump($order);die;
        // 商品信息
        
        foreach($order['goodsList'] as &$vv)
        {
            $goods_info = Db::name('shop_goods')->alias('g') 
            ->join('shop_goods_category c', 'g.category_id = c.id')
            ->where('g.id', $vv['id'])
            ->field('g.*, c.name as category_name')
            ->find();  
            
            $vv['is_gift'] = 0; // 是否赠品 0 否 1是
            $vv['goods_type'] = 2; // 
            $vv['ids'] = round(mt_rand(0, 1234567890));; // 随机数
            $vv['name'] = $goods_info['name']; // 商品名称
            $vv['is_coupon_goods'] = 0; // 是否优惠券商品 0否 1是
            $vv['is_project'] = 1; // 是否服务 1 是 2否
            $vv['kingdee_code'] = $goods_info['kingdee_code']; // 商品金蝶编码
            $vv['spec_name'] = $goods_info['spec_name']; // 商品规格
            $vv['goods_price'] = $goods_info['goods_price']; // 商品价格
            $vv['project_type'] = 1; // 
            $vv['prestock'] = [];
            $vv['checked'] = false;
            $vv['house_code'] = $info['kingdee_code'];
            $vv['service_number'] = ''; // 服务次数，服务卡选次数时必填
            $vv['start_time'] = ''; // 服务选天时 服务开始时间必填
            $vv['end_time'] = ''; //服务选天时 服务结束时间必填
            $vv['unit_code'] = $goods_info['unit_code']; // 商品金蝶销售单位编码
            $vv['littleCount'] = $goods_info['goods_price']; // 好像是价格
            $vv['serveData'] = [];
            $vv['prestock_type'] = 1; // 预存货类型 1 不存货 2 存货 3 出货
            $vv['business_type'] = 1; // 业务类型  1 疤痕 2项目 3青春痘 4 护肤品
            $vv['business_two_type'] = ''; // 业务类型  1 疤痕 2项目 3青春痘 4 护肤品
            $vv['status'] = 1; // 订单状态：0.待支付 1.已支付 2.已下推 3.已评价 4.已作废
            $vv['goods_sn'] = $goods_info['goods_sn']; // 货号
            $vv['category_id'] = $goods_info['category_id']; // 分类ID
            $vv['unit_name'] = $goods_info['unit_name']; // 商品单位名称
            $vv['create_time'] = $goods_info['create_time'];
            $vv['update_time'] = $goods_info['update_time'];
            $vv['shop_type'] = $goods_info['shop_type']; // 0公用，1门店，2门诊
            $vv['shoptype'] = $goods_info['shop_type']; // 0公用，1门店，2门诊
            $vv['zu_name'] = $goods_info['kingdee_code'].$goods_info['name']; // 组合名称
            $vv['category_name'] = $goods_info['category_name']; // 分类名称
            $vv['status_desc'] = $goods_info['status'] == 1 ? '上架' : '下架';
            $vv['project'] = $goods_info['is_project'] == 1 ? '产品' : '服务';
            $vv['shoptype'] = $goods_info['shop_type'] === '0' ? '公用' : ($goods_info['shop_type'] === '1' ? '门店' : ($goods_info['shop_type'] === '2' ? '门诊' : '')); 
            $vv['project_desc'] = $vv['is_project'] == 2 ? '是' : '否';
            $vv['house_code_name'] = $info['store_name'];
            $vv['desc'] = '商城创建订单';
            
        }
        // var_dump($order);
        // 你需要判定这个客户之前是不是新单 ；客户档案表中 cj_money 是否大于0如果大于0 显示补单，如果等于0 且本次订单收款金额大于等于500 就是新单 否则就是补单
        if($info['cj_money'] == 0 && $order['pay_amount'] >= 500)
        {
            $order['order_type'] = 1;
        }else{
            $order['order_type'] = 2;
        }
        // 查询销售员信息
        $sale = Db::name('shop_person')
            ->where('shop_id', $order['shop_id'])
            ->where('position_id', 'in', '10,9,8,7')
            ->where('status', 1)
            ->column('id,name', 'position_id');
        $selected_sale = null;
        // 优先选择店长
        if (isset($sale[7])) {
            $managers_sale = is_array(reset($sale[7])) ? $sale[7] : [ $sale[7] ];
            $randomIndex_sale = array_rand($managers_sale);
            $selected_sale = $managers_sale[$randomIndex_sale];
        } 
        // 选择店助
        elseif (isset($sale[10])) {
            $assistants_sale = is_array(reset($sale[10])) ? $sale[10] : [ $sale[10] ];
            $randomIndex_sale = array_rand($assistants_sale);
            $selected_sale = $assistants_sale[$randomIndex_sale];
        } 
        // 选择护理师
        elseif (isset($sale[9])) {
            $nurses_sale = is_array(reset($sale[9])) ? $sale[9] : [ $sale[9] ];
            $randomIndex_sale = array_rand($nurses_sale);
            $selected_sale = $nurses_sale[$randomIndex_sale];
        }
        // 选择主任
        elseif (isset($sale[8])) {
            $directors_sale = is_array(reset($sale[8])) ? $sale[8] : [ $sale[8] ];
            $randomIndex_sale = array_rand($directors_sale);
            $selected_sale = $directors_sale[$randomIndex_sale];
        } 
        $order['sale_id'] = $selected_sale['id'] ?? '';
        $order['sale_name'] = $selected_sale['name'] ?? '';
        
        $operator = Db::name('admin_user')
            ->alias('au')
            ->join('shop_person sp', 'au.shop_person_id = sp.id')
            ->where('au.shop_id', $order['shop_id'])
            ->where('au.status', 1)
            ->where('au.is_status', 0)
            ->where('sp.status', 1)
            ->where('sp.position_id', 'in', '10,9,8,7')
            ->column('au.id,au.realname','sp.position_id');
            
        $selected_operator = null;
        // 优先选择店助
        if (isset($operator[10])) {
            $assistants_operator = is_array(reset($operator[10])) ? $operator[10] : [ $operator[10] ];
            $randomIndex_operator = array_rand($assistants_operator);
            $selected_operator = $assistants_operator[$randomIndex_operator];
        } 
        // 选择店长
        elseif (isset($operator[7])) {
            $managers_operator = is_array(reset($operator[7])) ? $operator[7] : [ $operator[7] ];
            $randomIndex_operator = array_rand($managers_operator);
            $selected_operator = $managers_operator[$randomIndex_operator];
        } 
        // 选择护理师
        elseif (isset($operator[9])) {
            $nurses_operator = is_array(reset($operator[9])) ? $operator[9] : [ $operator[9] ];
            $randomIndex_operator = array_rand($nurses_operator);
            $selected_operator = $nurses_operator[$randomIndex_operator];
        }
        // 选择主任
        elseif (isset($operator[8])) {
            $directors_operator = is_array(reset($operator[8])) ? $operator[8] : [ $operator[8] ];
            $randomIndex_operator = array_rand($directors_operator);
            $selected_operator = $directors_operator[$randomIndex_operator];
        } 
        $order['operator_id'] = $selected_operator['id'] ?? '';
        $order['operator_name'] = $selected_operator['realname'] ?? '';
        // var_dump(json_encode($order));die;
        //请求创建订单接口
        $orderModel = new OrderModel();
        $data = $orderModel->addData($order);
        // var_dump($data);
        if(!$data)
        {
            return json(['msg'=> $orderModel->getError(),'code'=>0]);
        }else
        {
            // 查询订单ID
            $id = Db::name('shop_order')->where('api_order_id',$order['api_order_id'])->value('id');
            return json(['data'=>$id, 'code'=>1]);
        }
    }
    
    
    
	   
    // 创建退款单
    public function miniRefundOrder(Request $request)
    {
        $param = $request->param();
        if(empty($param['insert']))
        {
            return json(['msg'=>'缺少必传参数','code'=>0]);
        }else
        {
            $order = json_decode($param['insert'],1);
        }
        // 查询
        $info = Db::name('customer')
            ->alias('c') 
            ->where('c.id',$order['uid'])
            ->join('shop s', 'c.shop_id = s.shop_id') 
            ->where('c.id', $order['uid']) 
            ->field('c.id,c.shop_id,c.cj_money,s.shop_name, s.market_id, s.market_name, s.market_kingdee_code')
            ->find();
        $order['shop_id'] = $info['shop_id'];
        $order['shop_name'] = $info['shop_name'];
        $order['market_id'] = $info['market_id'];
        $order['market_name'] = $info['market_name'];
        $order['sales_organization'] = $info['market_kingdee_code'];
        $order['settlement_organization'] = $info['market_kingdee_code'];
        $order['sales_name'] = $info['market_name'];
        $order['settlement_name'] = $info['market_name'];
        // 你需要判定这个客户之前是不是新单 ；客户档案表中 cj_money 是否大于0如果大于0 显示补单，如果等于0 且本次订单收款金额大于等于500 就是新单 否则就是补单
        if($info['cj_money'] == 0 && $order['pay_amount'] >= 500)
        {
            $order['order_type'] = 1;
        }else{
            $order['order_type'] = 2;
        }
        // 查询销售员信息
        $sale = Db::name('shop_person')
            ->where('shop_id', $order['shop_id'])
            ->where('position_id', 'in', '10,9,8,7')
            ->where('status', 1)
            ->column('id,name', 'position_id');
        $selected_sale = null;
        // 优先选择店长
        if (isset($sale[7])) {
            $managers_sale = is_array(reset($sale[7])) ? $sale[7] : [ $sale[7] ];
            $randomIndex_sale = array_rand($managers_sale);
            $selected_sale = $managers_sale[$randomIndex_sale];
        } 
        // 选择店助
        elseif (isset($sale[10])) {
            $assistants_sale = is_array(reset($sale[10])) ? $sale[10] : [ $sale[10] ];
            $randomIndex_sale = array_rand($assistants_sale);
            $selected_sale = $assistants_sale[$randomIndex_sale];
        } 
        // 选择护理师
        elseif (isset($sale[9])) {
            $nurses_sale = is_array(reset($sale[9])) ? $sale[9] : [ $sale[9] ];
            $randomIndex_sale = array_rand($nurses_sale);
            $selected_sale = $nurses_sale[$randomIndex_sale];
        }
        // 选择主任
        elseif (isset($sale[8])) {
            $directors_sale = is_array(reset($sale[8])) ? $sale[8] : [ $sale[8] ];
            $randomIndex_sale = array_rand($directors_sale);
            $selected_sale = $directors_sale[$randomIndex_sale];
        } 
        $order['sale_id'] = $selected_sale['id'] ?? '';
        $order['sale_name'] = $selected_sale['name'] ?? '';
        
        $operator = Db::name('admin_user')
            ->alias('au')
            ->join('shop_person sp', 'au.shop_person_id = sp.id')
            ->where('au.shop_id', $order['shop_id'])
            ->where('au.status', 1)
            ->where('au.is_status', 0)
            ->where('sp.status', 1)
            ->where('sp.position_id', 'in', '10,9,8,7')
            ->column('au.id,au.realname','sp.position_id');
            
        $selected_operator = null;
        // 优先选择店助
        if (isset($operator[10])) {
            $assistants_operator = is_array(reset($operator[10])) ? $operator[10] : [ $operator[10] ];
            $randomIndex_operator = array_rand($assistants_operator);
            $selected_operator = $assistants_operator[$randomIndex_operator];
        } 
        // 选择店长
        elseif (isset($operator[7])) {
            $managers_operator = is_array(reset($operator[7])) ? $operator[7] : [ $operator[7] ];
            $randomIndex_operator = array_rand($managers_operator);
            $selected_operator = $managers_operator[$randomIndex_operator];
        } 
        // 选择护理师
        elseif (isset($operator[9])) {
            $nurses_operator = is_array(reset($operator[9])) ? $operator[9] : [ $operator[9] ];
            $randomIndex_operator = array_rand($nurses_operator);
            $selected_operator = $nurses_operator[$randomIndex_operator];
        }
        // 选择主任
        elseif (isset($operator[8])) {
            $directors_operator = is_array(reset($operator[8])) ? $operator[8] : [ $operator[8] ];
            $randomIndex_operator = array_rand($directors_operator);
            $selected_operator = $directors_operator[$randomIndex_operator];
        } 
        $order['operator_id'] = $selected_operator['id'] ?? '';
        $order['operator_name'] = $selected_operator['realname'] ?? '';
        //请求创建订单接口
        $refundModel = new RefundModel();
        $data  = $refundModel->addData($order);
        if(!$data)
        {
            return json(['msg'=> $refundModel->getError(),'code'=>0]);
        }else
        {
            // 查询订单ID
            $id = Db::name('shop_refund_order')->where('api_order_id',$order['api_order_id'])->value('id');
            return json(['data'=>$id, 'code'=>1]);
        }
    }
    
    
    
    // 创建退货单
    public function miniRefundGoods(Request $request)
    {
        $param = $request->param();
        if(empty($param['insert']))
        {
            return json(['msg'=>'缺少必传参数','code'=>0]);
        }else
        {
            $order = json_decode($param['insert'],1);
        }
        // 查询
        $info = Db::name('customer')
            ->alias('c') 
            ->where('c.id',$order['uid'])
            ->join('shop s', 'c.shop_id = s.shop_id') 
            ->where('c.id', $order['uid']) 
            ->field('c.id,c.shop_id,c.cj_money,s.shop_name, s.market_id, s.market_name, s.market_kingdee_code,s.store_name,s.store_kingdee_code')
            ->find();
        $order['shop_id'] = $info['shop_id'];
        $order['shop_name'] = $info['shop_name'];
        $order['market_id'] = $info['market_id'];
        $order['market_name'] = $info['market_name'];
        $order['sales_organization'] = $info['market_kingdee_code'];
        $order['settlement_organization'] = $info['market_kingdee_code'];
        $order['sales_name'] = $info['market_name'];
        $order['settlement_name'] = $info['market_name'];
        $order['delivery_king'] = $info['store_kingdee_code'];
        $order['house_code'] = $info['store_kingdee_code'];
        $order['house_code_name'] = $info['store_name'];
        $order['delivery_name'] = $info['shop_name'];
        // var_dump($order);die;
        // 商品信息
        
        foreach($order['goodsList'] as &$vv)
        {
            $goods_info = Db::name('shop_goods')->alias('g') 
            ->join('shop_goods_category c', 'g.category_id = c.id')
            ->where('g.id', $vv['id'])
            ->field('g.*, c.name as category_name')
            ->find();  
            $vv['prestock_type'] = 1; // 预存货类型 1 不存货 2 存货 3 出货
            $vv['goods_price'] = $goods_info['goods_price']; // 商品价格
            $vv['spec_name'] = $goods_info['spec_name']; // 商品规格
            $vv['unit_code'] = $goods_info['unit_code']; // 商品金蝶销售单位编码
            $vv['is_gift'] = 0; // 是否赠品 0 否 1是
            $vv['checked'] = false;
            $vv['pay_price'] = '';
            $vv['house_code'] = $info['store_kingdee_code'];
            $vv['house_code_name'] = $info['store_name'];
            $vv['business_two_type'] = ''; // 业务类型  1 疤痕 2项目 3青春痘 4 护肤品
            $vv['business_type'] = 4; // 业务类型  1 疤痕 2项目 3青春痘 4 护肤品
            $vv['prestock'] = [];
            $vv['descs'] = '商城退货';
            $vv['out_num'] = 0;
            $vv['gift_price'] = 0;
            $vv['shop_transfer_price'] = 0;
            $vv['is_project'] = 1; // 是否服务 1 是 2否
            $vv['name'] = [
                    'id' => $goods_info['id'],
                    'name' => $goods_info['name'],
                    'kingdee_code' => $goods_info['kingdee_code'],
                    'spec_name' => $goods_info['spec_name'],
                    'status' => $goods_info['status'],
                    'goods_price' => $goods_info['goods_price'],
                    'goods_sn' => $goods_info['goods_sn'],
                    'is_project' => $goods_info['is_project'],
                    'unit_code' => $goods_info['unit_code'],
                    'category_id' => $goods_info['category_id'],
                    'unit_name' => $goods_info['unit_name'],
                    'create_time' => $goods_info['create_time'],
                    'update_time' => $goods_info['update_time'],
                    'shop_type' => $goods_info['shop_type'],
                    'zu_name' => $goods_info['kingdee_code'].$goods_info['name'],
                    'category_name' => $goods_info['category_name'],
                    'project' => $goods_info['is_project'] == 1 ? '产品' : '服务',
                    'status_desc' =>  $goods_info['status'] == 1 ? '上架' : '下架',
                    'shoptype' =>  $goods_info['shop_type'] === '0' ? '公用' : ($goods_info['shop_type'] === '1' ? '门店' : ($goods_info['shop_type'] === '2' ? '门诊' : '')),
                    'project_desc' =>  $vv['is_project'] == 2 ? '是' : '否',
                ];
        }
        // var_dump($order);die;
        // 你需要判定这个客户之前是不是新单 ；客户档案表中 cj_money 是否大于0如果大于0 显示补单，如果等于0 且本次订单收款金额大于等于500 就是新单 否则就是补单
        if($info['cj_money'] == 0 && $order['pay_amount'] >= 500)
        {
            $order['order_type'] = 1;
        }else{
            $order['order_type'] = 2;
        }
        // 查询销售员信息
        $sale = Db::name('shop_person')
            ->where('shop_id', $order['shop_id'])
            ->where('position_id', 'in', '10,9,8,7')
            ->where('status', 1)
            ->column('id,name', 'position_id');
        $selected_sale = null;
        // 优先选择店长
        if (isset($sale[7])) {
            $managers_sale = is_array(reset($sale[7])) ? $sale[7] : [ $sale[7] ];
            $randomIndex_sale = array_rand($managers_sale);
            $selected_sale = $managers_sale[$randomIndex_sale];
        } 
        // 选择店助
        elseif (isset($sale[10])) {
            $assistants_sale = is_array(reset($sale[10])) ? $sale[10] : [ $sale[10] ];
            $randomIndex_sale = array_rand($assistants_sale);
            $selected_sale = $assistants_sale[$randomIndex_sale];
        } 
        // 选择护理师
        elseif (isset($sale[9])) {
            $nurses_sale = is_array(reset($sale[9])) ? $sale[9] : [ $sale[9] ];
            $randomIndex_sale = array_rand($nurses_sale);
            $selected_sale = $nurses_sale[$randomIndex_sale];
        }
        // 选择主任
        elseif (isset($sale[8])) {
            $directors_sale = is_array(reset($sale[8])) ? $sale[8] : [ $sale[8] ];
            $randomIndex_sale = array_rand($directors_sale);
            $selected_sale = $directors_sale[$randomIndex_sale];
        } 
        $order['sale_id'] = $selected_sale['id'] ?? '';
        $order['sale_name'] = $selected_sale['name'] ?? '';
        
        $operator = Db::name('admin_user')
            ->alias('au')
            ->join('shop_person sp', 'au.shop_person_id = sp.id')
            ->where('au.shop_id', $order['shop_id'])
            ->where('au.status', 1)
            ->where('au.is_status', 0)
            ->where('sp.status', 1)
            ->where('sp.position_id', 'in', '10,9,8,7')
            ->column('au.id,au.realname','sp.position_id');
            
        $selected_operator = null;
        // 优先选择店助
        if (isset($operator[10])) {
            $assistants_operator = is_array(reset($operator[10])) ? $operator[10] : [ $operator[10] ];
            $randomIndex_operator = array_rand($assistants_operator);
            $selected_operator = $assistants_operator[$randomIndex_operator];
        } 
        // 选择店长
        elseif (isset($operator[7])) {
            $managers_operator = is_array(reset($operator[7])) ? $operator[7] : [ $operator[7] ];
            $randomIndex_operator = array_rand($managers_operator);
            $selected_operator = $managers_operator[$randomIndex_operator];
        } 
        // 选择护理师
        elseif (isset($operator[9])) {
            $nurses_operator = is_array(reset($operator[9])) ? $operator[9] : [ $operator[9] ];
            $randomIndex_operator = array_rand($nurses_operator);
            $selected_operator = $nurses_operator[$randomIndex_operator];
        }
        // 选择主任
        elseif (isset($operator[8])) {
            $directors_operator = is_array(reset($operator[8])) ? $operator[8] : [ $operator[8] ];
            $randomIndex_operator = array_rand($directors_operator);
            $selected_operator = $directors_operator[$randomIndex_operator];
        } 
        $order['operator_id'] = $selected_operator['id'] ?? '';
        $order['operator_name'] = $selected_operator['realname'] ?? '';
        // var_dump($order);die;
        //请求创建订单接口
        $refundModel = new RefundModel();
        $data  = $refundModel->addGoodsData($order);
        if(!$data)
        {
            return json(['msg'=> $refundModel->getError(),'code'=>0]);
        }else
        {
            // 查询订单ID
            $id = Db::name('shop_refund_order')->where('api_order_id',$order['api_order_id'])->where('type',2)->value('id');
            return json(['data'=>$id, 'code'=>1]);
        }
    }
    
    
    
    
    
    
    
    
    protected function getCustomerModel()
    {
        return new CustomerModel();
    }
    protected function getPersonCustomer()
    {
        return new PersonCustomer();
    }
}